package com.wuyou.simple;

import com.sun.xml.internal.ws.util.StringUtils;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 1684. 统计一致字符串的数目
 */
public class CountConsistentStrings {


    public static void main(String[] args) {
//        String allowed = "ab";
//        String[] words = {"ad", "bd", "aaab", "baa", "badab"};
//        System.out.println(solution_4(allowed, words));
        Random random = new Random();
        double v = random.nextDouble();
        System.out.println(v);
        System.out.println(v + (1.0 / 5.0 / 2.0));
        System.out.println(v + (2.0 / 5.0 / 2.0));
        System.out.println(v + (3.0 / 5.0 / 2.0));
        System.out.println(v + (4.0 / 5.0 / 2.0));
        System.out.println(v + (5.0 / 5.0 / 2.0));
    }


    // 8ms
    public static int solution_4(String allowed, String[] words) {
        int res = 0;
        byte[] arr = new byte[26];
//        Arrays.fill(arr, -1); // 去掉后 6ms
        for (int i = 0; i < allowed.length(); i++) {
            arr[allowed.charAt(i) - 97] = 1;
        }
        for (int i = 0; i < words.length; i++) {

            boolean flag = true;
            for (int j = 0; j < words[i].length(); j++) {
                if (arr[words[i].charAt(j) - 97] != 1) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res++;
            }
        }
        return res;
    }

    // 71ms
    public static int solution_3(String allowed, String[] words) {
        List<Object> list = allowed.chars().mapToObj(c -> String.valueOf((char) c)).collect(Collectors.toList());
        list.add(allowed);
        int res = 0;
        for (int i = 0; i < words.length; i++) {
            boolean flag = true;
            if (list.contains(words[i])) {
                res++;
                continue;
            }
            for (int j = 0; j < words[i].length(); j++) {
                if (!list.contains(words[i].charAt(j) + "")) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res++;
            }
        }
        return res;
    }

    // 31ms
    public static int solution_2(String allowed, String[] words) {
        Map<String, Integer> map = new HashMap<>();
        int res = 0;
        for (int i = 0; i < words.length; i++) {
            boolean flag = true;
            if (map.get(words[i]) != null) {
                res++;
                continue;
            }
            if (allowed.contains(words[i])) {
                res++;
                map.put(words[i], 1);
                continue;
            }
            for (int j = 0; j < words[i].length(); j++) {
                if (!allowed.contains(words[i].charAt(j) + "")) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res++;
            }
        }
        return res;
    }

    // 32ms
    public static int solution_1(String allowed, String[] words) {

        int res = 0;
        for (int i = 0; i < words.length; i++) {
            boolean flag = true;
            for (int j = 0; j < words[i].length(); j++) {
                if (!allowed.contains(words[i].charAt(j) + "")) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res++;
            }
        }
        return res;
    }

}
